package cn.maihe.elg.operation.centers.epoint.channel.service;

import cn.maihe.elg.operation.centers.epoint.channel.EpointBaseChannelService;
import cn.maihe.elg.operation.centers.epoint.dto.EpointBaseRespDTO;
import cn.maihe.elg.operation.centers.epoint.dto.channel.MeiShanGiveupNoticeReq;
import cn.maihe.elg.operation.centers.epoint.enums.EpointChannelMethod;
import cn.maihe.elg.operation.centers.epoint.enums.EpointRespCodeEnum;
import cn.maihe.elg.operation.model.dto.InnerBaseResp;
import cn.maihe.elg.operation.model.enums.CenterNoEnum;
import cn.maihe.elg.operation.repository.entity.AcceptOrderInfoDO;
import cn.maihe.elg.operation.service.order.AcceptOrderInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * @Description 金融机构放弃申请
 * @Author SSX
 * @Date 2025/05/28
 */
@Service
@Slf4j
public class MeiShanGiveupNoticeChannelService extends EpointBaseChannelService<MeiShanGiveupNoticeReq, EpointBaseRespDTO> {
    @Resource
    private AcceptOrderInfoService acceptOrderInfoService;
    @Override
    protected EpointChannelMethod methodEnum() {
        return EpointChannelMethod.giveupnotice;
    }

    /**
     * 开票结果通知
     */

    public InnerBaseResp<EpointBaseRespDTO> giveupNotice(CenterNoEnum centerNoEnum, String acceptOrderNo) {
        AcceptOrderInfoDO acceptOrderInfoDO = acceptOrderInfoService.getByAcceptOrderNo(acceptOrderNo);
        if(Objects.isNull(acceptOrderInfoDO)){
            return InnerBaseResp.error(null,"查无此受理订单信息");
        }

        MeiShanGiveupNoticeReq req = new MeiShanGiveupNoticeReq();
        req.setApplyno(acceptOrderInfoDO.getCenterOrderId());

        InnerBaseResp<EpointBaseRespDTO> innerBaseResp;
        try {
            EpointBaseRespDTO resp = this.doRequest(centerNoEnum, req, null);
            if (EpointRespCodeEnum.isSuccess(resp.getCode())) {
                innerBaseResp = InnerBaseResp.success(null);
            } else {
                innerBaseResp = InnerBaseResp.error(resp.getMessage());
            }
        } catch (Exception e) {
            log.error("金融机构放弃申请>> [{}] 异常:{}", centerNoEnum.getCenterName(), e.getMessage(), e);
            innerBaseResp = InnerBaseResp.error("通知异常:" + e.getMessage());
        }

        return innerBaseResp;
    }
}
